package com.alibaba.aone.casestudy.module.action;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.CaseStudyConditions;
import com.alibaba.aone.casestudy.common.entity.Casestudy;
import com.alibaba.aone.casestudy.service.CasestudyService;
import com.alibaba.aone.casestudy.service.FailureService;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.dataresolver.Param;

/**
 * 类CaseStudyListAction.java的实现描述：TODO 类实现描述 
 * @author yongchun.chengyc 2011-4-28 下午09:26:21
 */
public class CaseStudyListAction {
	@Autowired
	private CasestudyService casestudyService;
	@Autowired
	private FailureService failureService;
	
	public void doSearch(
	@Param("initiator")String initiator,
	@Param("initDepartment")String initDepartment,
	@Param("status")Integer[] statusArray,
	@Param("category")Integer[] categoryArray,
	@Param("analysisTimeFrom")String analysisTimeFromTemp,
	@Param("analysisTimeTo")String analysisTimeToTemp,
	Context context
	) throws ParseException{
		CaseStudyConditions caseStudyConditions =  new CaseStudyConditions();
		List<Integer> statuses = new ArrayList<Integer>();
		statuses.addAll(Arrays.asList(statusArray));
		List<Integer> categories = new ArrayList<Integer>();
		categories.addAll(Arrays.asList(categoryArray));
		
		Date analysisTimeFrom = null;
		Date analysisTimeTo = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		analysisTimeFrom =  StringUtils.isEmpty(analysisTimeFromTemp) ? null:format.parse(analysisTimeFromTemp);
		analysisTimeTo = StringUtils.isEmpty(analysisTimeToTemp) ? null:format.parse(analysisTimeToTemp);
	
		
		caseStudyConditions.setInitiator(initiator);
		caseStudyConditions.setInitDepartment(initDepartment);
		
		System.out.println("<---------------------------------------------------------------------->");
		System.out.println(initDepartment);
		System.out.println("<---------------------------------------------------------------------->");
		caseStudyConditions.setCategories(categories);
		caseStudyConditions.setStatuses(statuses);
		caseStudyConditions.setAnalysisTimeFrom(analysisTimeFrom);
		caseStudyConditions.setAnalysisTimeTo(analysisTimeTo);
		List<Casestudy> caseStudies = casestudyService.search(caseStudyConditions);
		
		context.put("casestudies",caseStudies);
		context.put("failureService", failureService);
		
	}

}
